﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Infragistics.Win.UltraWinGrid;
using System.IO;
namespace RDD
{
    public partial class RepImpRem : Form
    {
        private DataSet dsGridDatos = new DataSet();
        Importacion mImpor = new Importacion();

        public RepImpRem()
        {
            InitializeComponent();
        }

        private void RepImpRem_Load(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                //Leer importaciones remitidas y pagadas
                dsGridDatos = mImpor.leerImpoRemitidas();
                grdDatos.DataSource = dsGridDatos;
                configurarGrid();
                grdDatos.DisplayLayout.Override.AllowAddNew = AllowAddNew.No;
                grdDatos.DisplayLayout.Override.AllowUpdate = Infragistics.Win.DefaultableBoolean.False;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ocurrio un error al crear el reporte :" + ex.Message);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
        private void configurarGrid()
        {
            string tFormatoSuma = "{0:###,##0}";
            #region Banda 0
            //Dar formato al grid banda 0 - Periodo
            UltraGridBand banda0 = grdDatos.DisplayLayout.Bands["IMPORT"];
            banda0.Override.HeaderAppearance.TextHAlign = Infragistics.Win.HAlign.Center;
            banda0.Columns["COMPANIA"].Header.Caption = "Compañia";
            banda0.Columns["AGENCIA"].Header.Caption = "Agencia";
            banda0.Columns["ESTADO"].Header.Caption = "Estado";
            //ID Importacion
            banda0.Columns["IMPORTID"].Header.Caption = "Importacion";
            banda0.Columns["IMPORTID"].CellAppearance.TextHAlign = Infragistics.Win.HAlign.Right;
            //Fecha
            banda0.Columns["FECHA"].Width = 100;
            banda0.Columns["FECHA"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.Date;
            banda0.Columns["FECHA"].CellAppearance.TextHAlign = Infragistics.Win.HAlign.Right;
            banda0.Columns["FECHA"].Header.Caption = "Fecha";
            //Valor remisionado
            banda0.Columns["VALORREMI"].Width = 100;
            banda0.Columns["VALORREMI"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.CurrencyNonNegative;
            banda0.Columns["VALORREMI"].CellAppearance.TextHAlign = Infragistics.Win.HAlign.Right;
            banda0.Columns["VALORREMI"].Format = "###,##0";
            banda0.Columns["VALORREMI"].Header.Caption = "Valor Remision";
            SummarySettings suma1 = banda0.Summaries.Add(SummaryType.Sum, banda0.Columns["VALORREMI"]);
            suma1.DisplayFormat = tFormatoSuma;
            suma1.Appearance.TextHAlign = Infragistics.Win.HAlign.Right;

            //Valor Reembolsado
            banda0.Columns["VALORREEM"].Width = 100;
            banda0.Columns["VALORREEM"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.CurrencyNonNegative;
            banda0.Columns["VALORREEM"].CellAppearance.TextHAlign = Infragistics.Win.HAlign.Right;
            banda0.Columns["VALORREEM"].Format = "###,##0";
            banda0.Columns["VALORREEM"].Header.Caption = "Pagado";
            SummarySettings suma2 = banda0.Summaries.Add(SummaryType.Sum, banda0.Columns["VALORREEM"]);
            suma2.DisplayFormat = tFormatoSuma;
            suma2.Appearance.TextHAlign = Infragistics.Win.HAlign.Right;

            //Diferencia
            banda0.Columns["DIFERENCIA"].Width = 100;
            banda0.Columns["DIFERENCIA"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.CurrencyNonNegative;
            banda0.Columns["DIFERENCIA"].CellAppearance.TextHAlign = Infragistics.Win.HAlign.Right;
            banda0.Columns["DIFERENCIA"].Format = "###,##0";
            banda0.Columns["DIFERENCIA"].Header.Caption = "Diferencia";
            SummarySettings suma3 = banda0.Summaries.Add(SummaryType.Sum, banda0.Columns["DIFERENCIA"]);
            suma3.DisplayFormat = tFormatoSuma;
            suma3.Appearance.TextHAlign = Infragistics.Win.HAlign.Right;
            //Saldo
            banda0.Columns["SALDO"].Width = 100;
            banda0.Columns["SALDO"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.CurrencyNonNegative;
            banda0.Columns["SALDO"].CellAppearance.TextHAlign = Infragistics.Win.HAlign.Right;
            banda0.Columns["SALDO"].Format = "###,##0";
            banda0.Columns["SALDO"].Header.Caption = "Saldo";
            SummarySettings suma4 = banda0.Summaries.Add(SummaryType.Sum, banda0.Columns["SALDO"]);
            suma4.DisplayFormat = tFormatoSuma;
            suma4.Appearance.TextHAlign = Infragistics.Win.HAlign.Right;
            //suma4.HasAppearance

            banda0.SortedColumns.Add("ESTADO", false, true);
            #endregion
        }

        private void cmdExportarExcel_Click(object sender, EventArgs e)
        {
            try
            {
                //Construir nombre de archivo
                string nombreArchivo = "C:/Softpro/RDD/Documentos/Remitidas a " + DateTime.Now.ToString("yyyyMMdd_HH_mm") + ".XLS";
                string directorio = "C:/Softpro/RDD/Documentos";
                //Verifica si existe el directorio. Si no, se crea.
                if (!Directory.Exists(directorio))
                {
                    Directory.CreateDirectory(directorio);
                }
                gexObjeto.Export(grdDatos, nombreArchivo);
                System.Diagnostics.Process.Start(nombreArchivo);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ocurrio un error al enviar reporte a Excel :" + ex.Message);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
    }
}
